<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'area_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    protected $appends = [
        'area', 'province_id', 'city_id'
    ];

    public function getAreaAttribute()
    {
        $area = DB::table('china_area')->where('code', $this->area_id)->first();
        $city = DB::table('china_area')->where('id', $area->parent_id)->first();
        $province = DB::table('china_area')->where('id', $city->parent_id)->first();
        return [
            'province' => $province->name,
            'city' => $city->name,
            'area' => $area->name,
            'province_id' => $province->code,
            'city_id' => $city->code,
            'area_id' => $area->code,
        ];
    }

    public function getProvinceIdAttribute()
    {
        return $this->area['province_id'];
    }

    public function getCityIdAttribute()
    {
        return $this->area['city_id'];
    }
}
